<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/myStrategyComment.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            //获取url传递过来的攻略id
            var param = getParams();
            //查询某个攻略的常用标签
            $.get('/strategies/' + param.id + '/tags', function (data) {
                $('.tag').renderValues(data, {
                    show: function (item, value) {
                        $(item).html('<input type="checkbox" autocomplete="off" value="' + value + '">' + value + '')
                    }
                })
            });

            //传递参数的时候禁止序列化
            jQuery.ajaxSettings.traditional = true;

            //用于接收星星的数量
            var starNum;
            //星星的点击事件
            $('.star i').click(function () {
                //获取该星星的序号
                starNum = $(this).data('num');
                //获取所有的星星
                var $star = $('.star i');
                //先把星星样式为空心
                $star.removeClass().addClass('fa fa-star-o fa-lg');
                //根据星星的数量改变样式
                for (var i = 0; i < starNum; i++) {
                    $($star[i]).removeClass().addClass('fa fa-star fa-lg');
                }
            });

            //提交表单
            $('#submitBtn').click(function () {

                //获取标签
                var tagArr = [];
                var index = 0;
                //获取选择的常用标签 的值
                var tags = $('label.active').children();
                for (var i = 0; i < tags.length; i++) {
                    tagArr[index] = tags[i].value;
                    index++;
                }
                //获取自定义标签
                var tagString = $('[name=myTags]').val();
                var myTags = tagString.split(" ");
                for (var i = 0; i < myTags.length; i++) {
                    if (myTags[i] != "") {
                        tagArr[index] = myTags[i];
                        index++;
                    }
                }
                ;

                //获取图片的url
                var img = $('#uploadImgs img');
                var urls = [];
                for (var i = 0; i < img.length; i++) {
                    urls[i] = $(img[i]).attr('src')
                }

                //封装传递的参数
                var data = {
                    starNum: starNum,
                    tags: tagArr,
                    content: $('[name=content]').val(),
                    imgs: urls
                };

                //提交表单
                $.post('/strategies/' + param.id + '/strategyComment', data, function (data) {
                    if (data.success) {
                        window.location.href = '/strategyCatalogs.html?id=' + param.id;
                    } else {
                        $(document).dialog({
                            content: data.msg
                        })
                    }
                })

            });


            //定义变量接收当前的上传图片的div控件
            var imgDiv;
            //图片按钮出文件上传框事件
            $('#uploadImgs div').click(function () {
                imgDiv = $(this);
                //触发文件上传框
                $('#btn-file').click();
            });

            //当文件上传框的值改变就发请求更新
            $('#fileForm').change(function () {
                //判断是否有上传文件
                if ($('#btn-file').val()) {
                    //提交表单
                    $('#fileForm').ajaxSubmit(function (data) {
                        //根据返回结果判断上传结果
                        if (data.status == 1) {
                            var img = '<img src=' + data.url + '>';
                            $(imgDiv).html(img)
                        } else {
                            $(document).dialog({
                                content: data.msg
                            })
                        }
                    })
                }
            })

        })
    </script>
</head>

<body>
<!-- 隐藏表单 ,用于图片上传 -->
<form id="fileForm" method="post" action="/images" enctype="multipart/form-data">
    <input type="file" id="btn-file" style="display: none" name="file">
</form>

<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>

        </div>
        <div class="col">
            <span>点评</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="comment">
    <div class="container star">
        <p>你对该旅游地的评价</p>
        <i class="fa fa-star-o fa-lg" data-num="1"></i>
        <i class="fa fa-star-o fa-lg" data-num="2"></i>
        <i class="fa fa-star-o fa-lg" data-num="3"></i>
        <i class="fa fa-star-o fa-lg" data-num="4"></i>
        <i class="fa fa-star-o fa-lg" data-num="5"></i>
    </div>
    <hr/>
    <div class="container tag">
        <p>请为该旅游地选择合适的标签（大家都在评）</p>
        <div class="btn-group-toggle" data-toggle="buttons" render-loop="list">
            <label class="btn btn-outline-danger " render-fun="show" render-key="list.name">
            </label>

        </div>

        <p>添加你自己的标签</p>
        <input name="myTags" class="form-control" placeholder="请使用空格隔开标签，每个标签不超8个字"/>

        <p>更多评价内容</p>
        <textarea name="content" class="form-control" rows="5" placeholder="请填写你对该旅游地的评价"></textarea>
    </div>
    <div class="container imgs ">
        <p>上传图片</p>

        <div class="row" id="uploadImgs">
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
        </div>
    </div>

</div>


<div class="operation">
    <button class="btn" onclick="window.history.go(-1)">放弃评价</button>
    <button class="btn" id="submitBtn">提交评价</button>
</div>


</body>


</html>